package com.medusa.aps.business.model.pagination;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import io.swagger.v3.oas.annotations.media.Schema;

import java.io.Serial;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * 分页查询参数
 *
 * @param <T> 查询数据类型
 * @author jinbu
 */

public class Page<T> implements IPage<T> {

    @Serial
    private static final long serialVersionUID = 8545996866326528798L;

    /**
     * 查询数据列表
     */
    @Schema(description = "查询出的数据列表", accessMode = Schema.AccessMode.READ_ONLY)
    protected List<T> records = Collections.emptyList();

    /**
     * 排序字段信息
     */
    @Schema(description = "排序字段信息")
    protected List<OrderItem> orders = new ArrayList<>();

    /**
     * 总数
     */
    @Schema(description = "总数", accessMode = Schema.AccessMode.READ_ONLY)
    protected long total = 0;

    /**
     * 每页显示条数，默认 10
     */
    @Schema(description = "每页显示条数，默认 10")
    protected long size = 10;

    /**
     * 当前页
     */
    @Schema(description = "当前页")
    protected long current = 1;

    public Page() {
    }

    /**
     * 分页构造函数
     *
     * @param current 当前页
     * @param size    每页显示条数
     */
    public Page(long current, long size) {
        this(current, size, 0);
    }


    public Page(long current, long size, long total) {
        if (current > 1) {
            this.current = current;
        }
        this.size = size;
        this.total = total;
    }

    public Page(long current, long size, long total, List<T> records) {
        if (current > 1) {
            this.current = current;
        }
        this.size = size;
        this.total = total;
        this.records = records;
    }

    @Override
    public List<OrderItem> orders() {
        return this.orders;
    }

    /**
     * 自动优化 COUNT SQL【 默认：true 】
     *
     * @return true 是 / false 否
     */
    @Override
    public boolean optimizeCountSql() {
        return IPage.super.optimizeCountSql();
    }

    /**
     * {@link PaginationInnerInterceptor#isOptimizeJoin()}
     * 两个参数都为 true 才会进行sql处理
     *
     * @return true 是 / false 否
     * @since 3.4.4 @2021-09-13
     */
    @Override
    public boolean optimizeJoinOfCountSql() {
        return IPage.super.optimizeJoinOfCountSql();
    }

    /**
     * 进行 count 查询 【 默认: true 】
     *
     * @return true 是 / false 否
     */
    @Override
    public boolean searchCount() {
        return IPage.super.searchCount();
    }

    /**
     * 计算当前分页偏移量
     */
    @Override
    public long offset() {
        return IPage.super.offset();
    }

    /**
     * 最大每页分页数限制,优先级高于分页插件内的 maxLimit
     *
     * @since 3.4.0 @2020-07-17
     */
    @Override
    public Long maxLimit() {
        return IPage.super.maxLimit();
    }

    /**
     * 当前分页总页数
     */
    @Override
    public long getPages() {
        return IPage.super.getPages();
    }

    /**
     * 内部什么也不干
     * <p>只是为了 json 反序列化时不报错</p>
     *
     * @param pages
     */
    @Override
    public IPage<T> setPages(long pages) {
        return IPage.super.setPages(pages);
    }

    public void setOrders(List<OrderItem> orders) {
        this.orders = orders;
    }

    @Override
    public List<T> getRecords() {
        return this.records;
    }

    @Override
    public IPage<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    @Override
    public long getTotal() {
        return this.total;
    }

    @Override
    public IPage<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    @Override
    public long getSize() {
        return this.size;
    }

    @Override
    public IPage<T> setSize(long size) {
        this.size = size;
        return this;
    }

    @Override
    public long getCurrent() {
        return this.current;
    }

    @Override
    public IPage<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    @Override
    @SuppressWarnings("unchecked")
    public <R> Page<R> convert(Function<? super T, ? extends R> mapper) {
        return (Page<R>) IPage.super.convert(mapper);
    }

    /**
     * 老分页插件不支持
     * <p>
     * MappedStatement 的 id
     *
     * @return id
     * @since 3.4.0 @2020-06-19
     */
    @Override
    public String countId() {
        return IPage.super.countId();
    }


}
